import Toast from '../../../../../miniprogram_npm/@vant/weapp/toast/toast'
import Dialog from  '../../../../../miniprogram_npm/@vant/weapp/dialog/dialog'
const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    image: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const ctx = wx.createCameraContext()
    ctx.setZoom({
      zoom: 1,
      complete: res => {
        log.l(JSON.stringify(res))
      }
    })
  },

  /**
   * 拍照
   * @param e
   */
  onCamera: function(e){
    const _this = this
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: res => {
        _this.setData({
          image: res.tempImagePath
        })
        log.l("照片地址：" + res.tempImagePath)
      }
    })
  },

  /**
   * 确定
   * @param e
   */
  onConfirm: function(e){
    Toast.loading({
      duration: 0,
      mask: true,
      message: '正在上传'
    })
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
    http.upFile(this.data.image,'head').then(res1 => {
      if (res1.code === http.REQUEST_OK) {
        Toast.loading({
          duration: 0,
          mask: true,
          message: '正在检测'
        })
        http.get('people_info/check/getCertificationAvatar',{
          // img_url: 'http://files.xinwangzn.com/files/headimg/20207/27/ff8080816ee07ef201738ede3ba507eb.jpg'
          img_url: res1.data.url
        }).then(res2 => {
          if (res2.data.code === http.REQUEST_OK){
            wx.startFacialRecognitionVerify({
              name: app.globalData.cardInfo.name,
              idCardNumber:  app.globalData.cardInfo.id,
              checkAliveType: 2,
              complete: res123 => {
                Toast.success({
                duration: 1000,
                message: '检测成功'
              })
                app.globalData.peopleImageUrl = res1.data.url
                app.globalData.isAuthentication = true
                prevPage.setData({
                  peopleImageUrl: app.globalData.peopleImageUrl
                })
                setTimeout(() => {
                  Toast.clear()
                  wx.navigateBack()
                },1000)
              },
              fail(res1) {
                Toast.fail("认证失败！" + res1.data.errMsg);
                log.e(res1.data.errCode)
              }
            })
          }else {
            Dialog.alert({
              title: '温馨提示',
              message: '检测失败，请拍摄清晰人脸照片'
            }).then(() => {
              this.setData({
                image: null
              })
            })
          }
        },err => {
          Toast.clear()
          Dialog.alert({
            title: '温馨提示',
            message: '上传失败，请重新拍摄'
          }).then(() => {
            this.setData({
              image: null
            })
          })
        })
      }else {
        Toast.clear()
        Dialog.alert({
          title: '温馨提示',
          message: '上传失败，请重新拍摄'
        }).then(() => {
          this.setData({
            image: null
          })
        })
        // Toast.fail('上传失败，请重新拍摄')
      }
    },err => {
      // Toast.fail('上传失败，请重新拍摄')
      Toast.clear()
      Dialog.alert({
        title: '温馨提示',
        message: '上传失败，请重新拍摄'
      }).then(() => {
        this.setData({
          image: null
        })
      })
    })
  },

  /**
   * 关闭 退出
   * @param e
   */
  onBack: function(e){
    if (this.data.image == null){
      wx.navigateBack()
    }else {
      this.setData({
        image: null
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})